package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.domain.AdmDocker;
import com.dss.auto_deploy.domain.Server;
import com.dss.auto_deploy.dto.AdmDockerAddDto;
import com.dss.auto_deploy.dto.AdmDockerPageDto;
import com.dss.auto_deploy.dto.AdmDockerUpdateDto;
import com.dss.auto_deploy.dto.DockerInstallDto;
import com.dss.auto_deploy.service.IAdmDockerService;
import com.dss.auto_deploy.service.IServerService;
import com.dss.auto_deploy.util.Result;
import com.dss.auto_deploy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
* docker界面地址
* @Description:docker界面地址控制层
* @author dongshoushan
* @date 2022-09-22 14:38:54
*/
@RestController
@RequestMapping("/admDocker")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: docker管理")
public class AdmDockerController  {

    /**
    * docker界面地址Service
    */
    private final  IAdmDockerService admDockerService;
    private final IServerService serverService;

    /**
    * 新增docker界面地址
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增docker界面地址", notes="新增docker界面地址")
    public Result insert(@RequestBody @Valid AdmDockerAddDto addDto) {
        admDockerService.insert(addDto);
        return Result.success("新增成功");
    }

    /**
    * 修改docker界面地址
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改docker界面地址", notes="修改docker界面地址")
    public Result update(@RequestBody @Valid AdmDockerUpdateDto updateDto) {
        admDockerService.update(updateDto);
        return Result.success("修改成功");
    }
    @ApiOperation(value = "离线安装docker", notes="离线安装docker")
    @PostMapping(value = "install", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result install(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestParam(value = "portainerPort") String portainerPort,
            @RequestParam(value = "registryId",required = false) String registryId,
            @RequestPart(value = "dockerFile") MultipartFile dockerFile,
            @RequestPart(value = "portainerFile") MultipartFile portainerFile,
            HttpServletRequest request) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        DockerInstallDto installDto = DockerInstallDto.builder().ids(ids).tempPath(tempPath).portainerPort(portainerPort)
                .registryId(StringUtils.isEmpty(registryId)?null:Long.parseLong(registryId)).dockerFile(dockerFile).portainerFile(portainerFile).build();
        admDockerService.install(installDto);
        return Result.success();
    }

    /**
    * 批量删除docker界面地址
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除docker界面地址", notes="删除docker界面地址")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admDockerService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
    * 分页查询docker界面地址
    * @param pageDto
    * @return
    */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询docker", notes="分页查询docker")
    public  Result page(AdmDockerPageDto pageDto){
        return Result.success(admDockerService.list(pageDto));
    }
    /**
     * 查询docker
     * @param
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询docker", notes="查询docker")
    public List<AdmDocker> getAll(String name){
        return admDockerService.getAll(name);
    }
    /**
     * 根据id查询docker
     * @param
     * @return
     */
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据id查询docker", notes="根据id查询docker")
    public Result getById(@PathVariable Long id){
        return Result.success(admDockerService.getVoById(id));
    }
    /**
     * 根据ip查询docker
     * @param
     * @return
     */
    @GetMapping("/getByIp/{ip}")
    @ApiOperation(value = "根据ip查询docker", notes="根据ip查询docker")
    public Result getByIp(@PathVariable String ip){
        return Result.success(admDockerService.getByIp(ip));
    }

    /**
     * 查询终端
     * @param
     * @return
     */
    @GetMapping("/getServerByDockerId/{id}")
    @ApiOperation(value = "查询终端", notes="查询终端")
    public Result getServerByDockerId(@PathVariable Long id){
        AdmDocker docker = admDockerService.getById(id);
        Server server = serverService.getServerById(docker.getServerId());
        return Result.success(server);
    }
}
